package com.hifar.test.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.enums.ReportTypeEnum;
import com.hifar.plat.baseModal.pojo.BaseOssFiles;
import com.hifar.plat.baseModal.service.IBaseOssFilesService;
import com.hifar.sys.MinioUtil;
import com.hifar.test.constant.FileRefTypeConstant;
import com.hifar.test.template.mapper.HfEnvTemplateMapper;
import com.hifar.test.template.pojo.HfEnvTemplate;
import com.hifar.test.template.pojo.VO.HfEnvTemplateVO;
import com.hifar.test.template.service.IHfEnvTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author zhuWeiWei
 * @description 模板管理
 * @date 2024/12/27 15:47
 */
@Service
public class HfEnvTemplateServiceImpl extends ServiceImpl<HfEnvTemplateMapper, HfEnvTemplate> implements IHfEnvTemplateService {
    @Autowired
    private IBaseOssFilesService baseOssFilesService;


    @Override
    public void saveTemplate(HfEnvTemplateVO envTemplateVO) {
        HfEnvTemplate hfEnvTemplate = new HfEnvTemplate();
        BeanUtils.copyProperties(envTemplateVO, hfEnvTemplate);
        this.save(hfEnvTemplate);
        String id = hfEnvTemplate.getId();

        List<BaseOssFiles> files = envTemplateVO.getFiles();
        if (!files.isEmpty()) {
            BaseOssFiles baseOssFiles = files.get(0);
            Assert.notNull(baseOssFiles, "文件不存在!");
            String filesId = baseOssFiles.getId();
            BaseOssFiles ossFiles = baseOssFilesService.getById(filesId);
            Assert.notNull(baseOssFiles, "文件不存在!");
            String filePath = ossFiles.getFilePath();
            String templateType = envTemplateVO.getTemplateType();
            ReportTypeEnum reportTypeEnum = ReportTypeEnum.findEnumByCode(templateType);
            Assert.notNull(reportTypeEnum, "模板类型不正确");
            int maxSize = reportTypeEnum.getMaxSize();
            if (maxSize > 0) {
                int count = this.countTemplateNotId(templateType, id);
                Assert.isTrue(maxSize > count, "模版已超上限");
            }
            hfEnvTemplate.setTemplateTypeName(reportTypeEnum.getTitle());
            hfEnvTemplate.setTemplatePath(filePath);
            this.updateById(hfEnvTemplate);
            baseOssFiles.setRefType(FileRefTypeConstant.TEMPLATE_REF_TYPE);
            baseOssFiles.setRefId(id);
            baseOssFilesService.updateById(baseOssFiles);
        }
    }

    @Override
    public void editTemplate(HfEnvTemplateVO envTemplateVO) {
        String id = envTemplateVO.getId();
        HfEnvTemplate hfEnvTemplate = new HfEnvTemplate();
        BeanUtils.copyProperties(envTemplateVO, hfEnvTemplate);
        Assert.hasText(id, "id不能为空!");

        List<BaseOssFiles> files = envTemplateVO.getFiles();
        if (!files.isEmpty()) {
            BaseOssFiles baseOssFiles = files.get(0);
            String filesId = baseOssFiles.getId();
            BaseOssFiles ossFiles = baseOssFilesService.getById(filesId);
            Assert.notNull(baseOssFiles, "文件不存在!");
            String filePath = ossFiles.getFilePath();
            String templateType = envTemplateVO.getTemplateType();
            ReportTypeEnum reportTypeEnum = ReportTypeEnum.findEnumByCode(templateType);
            Assert.notNull(reportTypeEnum, "模板类型不正确");
            int maxSize = reportTypeEnum.getMaxSize();
            if (maxSize > 0) {
                int count = this.countTemplateNotId(templateType, id);
                Assert.isTrue(maxSize > count, "模版已超上限");
            }
            hfEnvTemplate.setTemplateTypeName(reportTypeEnum.getTitle());
            hfEnvTemplate.setTemplatePath(filePath);
            this.updateById(hfEnvTemplate);
            baseOssFiles.setRefType(FileRefTypeConstant.TEMPLATE_REF_TYPE);
            baseOssFiles.setRefId(id);

            UpdateWrapper<BaseOssFiles> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                    .set(BaseOssFiles::getRefId, null)
                    .eq(BaseOssFiles::getRefId, id)
                    .eq(BaseOssFiles::getRefType
                            , FileRefTypeConstant.TEMPLATE_REF_TYPE);
            baseOssFilesService.update(updateWrapper);
            baseOssFilesService.updateById(baseOssFiles);
        }


    }

    @Override
    public int countTemplate(String templateType) {
        QueryWrapper<HfEnvTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvTemplate::getTemplateType, templateType);
        return this.count(queryWrapper);
    }

    @Override
    public int countTemplateNotId(String templateType, String id) {
        QueryWrapper<HfEnvTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvTemplate::getTemplateType, templateType)
                .ne(HfEnvTemplate::getId, id);
        return this.count(queryWrapper);
    }

    @Override
    public HfEnvTemplateVO queryVoById(String id) {
        HfEnvTemplateVO templateVO = new HfEnvTemplateVO();
        HfEnvTemplate envTemplate = getById(id);
        Assert.notNull(envTemplate, "数据不存在!");
        BeanUtils.copyProperties(envTemplate, templateVO);
        List<BaseOssFiles> baseOssFiles = MinioUtil.listByRefId(id, FileRefTypeConstant.TEMPLATE_REF_TYPE);
        templateVO.setFiles(baseOssFiles);
        String templateType = envTemplate.getTemplateType();
        String templateTypeName = ReportTypeEnum.queryTitle(templateType);
        envTemplate.setTemplateName(templateTypeName);
        return templateVO;
    }

    @Override
    public HfEnvTemplate getReportTemplate() {
        QueryWrapper<HfEnvTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvTemplate::getTemplateType, ReportTypeEnum.REPORT.getValue());
        return getOne(queryWrapper);
    }

    @Override
    public HfEnvTemplate getTemplateByType(ReportTypeEnum typeEnum) {
        QueryWrapper<HfEnvTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvTemplate::getTemplateType, typeEnum.getValue());
        return getOne(queryWrapper);
    }
}
